package com.nervenets.web.controller.handler.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nervenets.utils.StringUtils;
import com.nervenets.web.controller.Global;
import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.enumeration.*;
import com.nervenets.web.hibernate.domain.setting.Tags;
import com.nervenets.web.redis.domain.global.GlobalRule;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class TagsAllHandler extends Handler<EmptyParams> {
    @Override
    public String handle(EmptyParams params, HttpServletRequest request) throws Exception {
        return getTags() + getVars();
    }

    @Override
    public String result() throws Exception {
        return null;
    }

    private String getVars() {
        final GlobalRule globalRule = globalVariablesService.getGlobalRule();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("defaultAvatar", globalRule.getDefaultAvatar());
//        jsonObject.put("proportionOfIssueWithdraw", globalRule.getProportionOfIssueWithdraw());
//        jsonObject.put("proportionOfMarketWithdraw", globalRule.getProportionOfMarketWithdraw());
//        jsonObject.put("proportionOfDealerWithdraw", globalRule.getProportionOfDealerWithdraw());
        jsonObject.put("proportionOfUserWithdraw", globalRule.getProportionOfUserWithdraw());
        jsonObject.put("proportionOfReward2Money", globalRule.getProportionOfReward2Money());
        jsonObject.put("proportionOfFriends", globalRule.getProportionOfFriends());
        jsonObject.put("proportionOfTeam", globalRule.getProportionOfTeam());
        jsonObject.put("movieStatus", MovieStatus.values());
        jsonObject.put("movieReleaseStatus", MovieReleaseStatus.values());
        jsonObject.put("movieType", MovieType.values());
        jsonObject.put("payType", PayType.values());
        jsonObject.put("orderType", OrderType.values());
        jsonObject.put("orderStatus", OrderStatus.values());
        jsonObject.put("tailConfirmHour", globalRule.getTailConfirmHour());
        jsonObject.put("orderExpiredPayTime", globalRule.getOrderExpiredPayTime());
        return "var vars = " + StringUtils.toJson(jsonObject) + ";";
    }

    private String getTags() throws Exception {
        String managerJson = globalService.getGlobalPair(Global.Constants.TAGS_KEY);
        if (!StringUtils.isEmpty(managerJson)) {
            return managerJson;
        }
        String hql = "from Tags ";
        List<Tags> allTags = userDao.getAllObjectsByHql(hql, new ArrayList<>());
        JSONObject jsonObject = new JSONObject();
        for (Tags tag : allTags) {
            if (0 == tag.getRemoved()) {
                String tagType = tag.getTagType().name();
                if (!jsonObject.containsKey(tagType)) {
                    jsonObject.put(tagType, new JSONArray());
                }
                jsonObject.getJSONArray(tagType).add(new TagModel(tag));
            }
            jsonObject.put(String.valueOf(tag.getId()), new TagModel(tag));
        }

        final String result = "var tags = " + jsonObject.toJSONString() + ";";
        globalService.setGlobalPair(Global.Constants.TAGS_KEY, result);
        return result;
    }

    class TagModel extends JSONObject {

        TagModel(Tags tags) {
            put("tagId", tags.getId());
            put("text", tags.getName());
            put("icon", tags.getIcon());
            put("parentId", tags.getParentId());
            put("queue", tags.getQueue());
        }
    }
}
